package net.mihome.app.adapter;

import java.util.ArrayList;
import java.util.List;

import com.google.zxing.oned.rss.FinderPattern;

import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.bean.ChatRoom;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.ui.MihomeActivisActivity;
import net.mihome.app.R;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ChatRoomListAdapter extends BaseAdapter {
	private List<ChatRoom> list=new ArrayList<ChatRoom>();
	private Context context;
	private AppContext appContext;
	private int type;
	private Handler handler;
	public ChatRoomListAdapter(Context context,List<ChatRoom> list,Handler handler,int type) {
		this.context = context;
		this.list=list;
		this.type=type;
		appContext=(AppContext) context.getApplicationContext();
		this.handler=handler;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0).getId();
	}

	@Override
	public View getView(int postion, View view, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		ChatRoom chatRoom=list.get(postion);
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.chat_room_list_item, null);
		view.setTag(chatRoom);
		TextView textView=(TextView) view.findViewById(R.id.rss_type_item_tv_id);
		textView.setText(chatRoom.getName());
		//
		TextView createAtByTextView=(TextView) view.findViewById(R.id.chat_room_list_create_at);
		createAtByTextView.setText(chatRoom.getCreate_at());

		TextView createByByTextView=(TextView) view.findViewById(R.id.chat_room_list_create_by);
		createByByTextView.setText(chatRoom.getCreate_by_name());

		TextView descTextView=(TextView) view.findViewById(R.id.chat_room_item_desc);
		descTextView.setText("活动内容:"+chatRoom.getDesc());
		TextView timeTextView=(TextView) view.findViewById(R.id.chat_room_item_time);
		timeTextView.setText("活动时间:"+chatRoom.getTime());
		TextView peopelnumByTextView=(TextView) view.findViewById(R.id.chat_room_item_peopelnum);
		peopelnumByTextView.setText("活动人数:"+chatRoom.getPeopelNum());
		//
		Button del=(Button) view.findViewById(R.id.chat_room_item_btn_del);
		Button join=(Button) view.findViewById(R.id.chat_room_item_btn_join);
		Button out=(Button) view.findViewById(R.id.chat_room_item_btn_out);
		del.setTag(chatRoom);
		join.setTag(chatRoom);
		out.setTag(chatRoom);
		
		addDelLinser(del);
		addJoinLinser(join);
		addOutLinser(out);
		if(type==ChatRoom.ALL){
			if(!chatRoom.getIsJoin()){
				out.setVisibility(View.GONE);
			}else{
				out.setVisibility(View.VISIBLE);
			}
		}else if(type==ChatRoom.MY){
			join.setVisibility(View.GONE);
		}
		//
		return view;
	}
	public void addDelLinser(Button button){
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
					try {
						Button button=(Button) arg0;
						ChatRoom chatRoom=(ChatRoom) button.getTag();
						ResponsePojo responsePojo=appContext.delRoom(chatRoom.getId()+"");
						if(responsePojo.getStatus()<0){
							Toast.makeText(context,responsePojo.getMsg(), Toast.LENGTH_SHORT).show();
						}else{
							handler.sendEmptyMessage(MihomeActivisActivity.HANDER_MSG_INITDATA);
						}
					} catch (AppException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(context,e.getMessage(), Toast.LENGTH_SHORT).show();
					}
			}
		});
	}
	public void addJoinLinser(Button button){
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(final View arg0) {
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						try {
							handler.sendEmptyMessage(MihomeActivisActivity.SHOW_LOADING_DIALOG);
							Button button=(Button) arg0;
							ChatRoom chatRoom=(ChatRoom) button.getTag();
							ResponsePojo responsePojo=appContext.JoinRoom(chatRoom.getId()+"");
							if(responsePojo.getStatus()<0){
								Toast.makeText(context,responsePojo.getMsg(), Toast.LENGTH_SHORT).show();
							}else{
								handler.sendEmptyMessage(MihomeActivisActivity.HANDER_MSG_INITDATA);
							}
						} catch (AppException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							Toast.makeText(context,e.getMessage(), Toast.LENGTH_SHORT).show();
							
						}
					}
				}).start();
			}
		});
	}
	public void addOutLinser(Button button){
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(final View arg0) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						try {
							handler.sendEmptyMessage(MihomeActivisActivity.SHOW_LOADING_DIALOG);
							Button button=(Button) arg0;
							ChatRoom chatRoom=(ChatRoom) button.getTag();
							ResponsePojo responsePojo=appContext.leavelRoom(chatRoom.getId()+"");
							if(responsePojo.getStatus()<0){
								Toast.makeText(context,responsePojo.getMsg(), Toast.LENGTH_SHORT).show();
							}else{
								handler.sendEmptyMessage(MihomeActivisActivity.HANDER_MSG_INITDATA);
							}
						} catch (AppException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							Toast.makeText(context,e.getMessage(), Toast.LENGTH_SHORT).show();
						}
					}
				}).start();
			}
		});
	}
}
